package com.tiger.cloud.controller.crm;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiger.cloud.common.mapper.JsonMapper;
import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.model.crm.Commucation;
import com.tiger.cloud.service.crm.ICommucationService;
import com.tiger.cloud.util.DateTimeUtil;
import com.tiger.cloud.util.ErrorCode;
import com.tiger.cloud.util.Page;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 要事纪要
 */
@Controller
@RequestMapping(value = "commucation")
@Api(value = "要事纪要",tags = "要事纪要")
public class CommucationController extends BaseController {

	@Autowired
	private ICommucationService commucationService;
	
	/**
	 * 增量获取要事纪要
	 */
	@RequestMapping(value = "incrementRecommands", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "增量获取要事纪要",notes = "增量获取要事纪要")
	public Response incrementRecommands(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody Commucation commucation){
		commucation.setIsDelete(DeleteEnum.NOMAIL);
		commucation.setUserId(userId);
		List<Commucation> recommandLists =commucationService.find(commucation);
		return ResponseUtil.ok(recommandLists);
	}
	
	/**
	 * 增量分页获取要事纪要
	 */
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "增量分页获取要事纪要",notes = "增量分页获取要事纪要")
	public Response find(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody Commucation commucation){
		commucation.setUserId(userId);
		commucation.setIsDelete(DeleteEnum.NOMAIL);
		Page<Commucation> datas=commucationService.page(commucation);
		datas.setCurrentTime(DateTimeUtil.getSystemDate());
		return ResponseUtil.ok(datas);
	}
	/**
	 * 增量分页获取要事纪要
	 */
	@RequestMapping(value = "findByGroupCustomer", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "增量分页获取要事纪要",notes = "增量分页获取要事纪要")
	public Response findByGroupCustomer(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody Commucation commucation){
		commucation.setUserId(userId);
		commucation.setIsDelete(DeleteEnum.NOMAIL);
		Page<Commucation> datas=commucationService.findByGroupCustomer(commucation);
		datas.setCurrentTime(DateTimeUtil.getSystemDate());
		return ResponseUtil.ok(datas);
	}
	
	@RequestMapping(value = "get", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "根据ID获取单条要事纪要",notes = "根据ID获取单条要事纪要")
	public Response get(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody Commucation commucation){
		if(commucation.getId()!=null&&commucation.getId()<0){
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID,"id必传!");
		}
		if(commucation.getCustomerType()==null){
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID,"customerType必传!");
		}
		Commucation findCommucation=commucationService.getCommucation(commucation);
		if(findCommucation.getUserId()!=userId){
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID,"只能查看自己的数据!");
		}
		return ResponseUtil.ok(findCommucation);
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "更新用户要事纪要",notes = "更新用户要事纪要")
	public Response update(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody Commucation commucation){
		if(commucation.getId()!=null&&commucation.getId()>0){
			Commucation updateEntity=commucationService.get(commucation.getId());
			if(updateEntity==null||updateEntity.getUserId()!=userId){
				return ResponseUtil.error("参数错误,请重试!");
			}
			commucation.setUserId(userId);
			if(commucationService.update(commucation)>0){
				return ResponseUtil.ok();
			};
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加用户要事纪要",notes = "添加用户要事纪要")
	public Response add(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody Commucation commucation){
		commucation.setUserId(userId);
		commucation.setIsDelete(DeleteEnum.NOMAIL);
		System.err.println("--------------------"+JsonMapper.nonEmptyMapper().toJson(commucation));
		if(commucationService.insertAndUpdateCommunicatTime(commucation)){
			return ResponseUtil.ok(commucation);
		}
		return ResponseUtil.error();
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除用户要事纪要",notes = "标记删除用户要事纪要")
	public Response delete(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody Commucation commucation){
		if(commucation.getId()!=null&&commucation.getId()>0){
			Commucation deleteEntity=commucationService.get(commucation.getId());
			if(deleteEntity==null||deleteEntity.getUserId()!=userId){
				return ResponseUtil.error("参数错误,请重试!");
			}
			deleteEntity.setIsDelete(DeleteEnum.DELETE);
			if(commucationService.update(deleteEntity)>0){
				return ResponseUtil.ok();
			};
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}

}